package com.youlai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.mapper.OrderItemMapper;
import com.youlai.system.mapper.OrderMapper;
import com.youlai.system.model.dto.OrderDto;
import com.youlai.system.model.dto.OrderItemDto;
import com.youlai.system.model.entity.Order;
import com.youlai.system.model.entity.OrderItem;
import com.youlai.system.model.vo.OrderItemVO;
import com.youlai.system.model.vo.OrderVO;
import com.youlai.system.service.OrderItemService;
import com.youlai.system.service.OrderService;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cai
 * @create 2024-03-11 20:51
 */
@Service
@RequiredArgsConstructor
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {

    @Override
    public PageResult<OrderItemVO> getPage(OrderItemDto orderItemDto) {
        int pageNum = orderItemDto.getPageNum();
        int pageSize = orderItemDto.getPageSize();
        String title = orderItemDto.getTitle();
        Integer orderId = orderItemDto.getOrderId();

        Page<OrderItem> page = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<OrderItem>()
                        .like(StringUtil.isNotBlank(title),OrderItem::getTitle , title)
                        .eq(orderId != null,OrderItem::getOrderId,orderId)
        );

        List<OrderItemVO> orderItemVOList = new ArrayList<>();
        page.getRecords().forEach(orderItem -> {
            OrderItemVO orderItemVO = new OrderItemVO();
            BeanUtils.copyProperties(orderItem, orderItemVO);
            orderItemVOList.add(orderItemVO);
        });

        return PageResult.success(orderItemVOList, page.getTotal());
    }
}
